<script>
	import Vue from 'vue'
	export default {
		onLaunch: function() {
			uni.getSystemInfo({
				success: function(e) {
					// #ifndef MP
					Vue.prototype.StatusBar = e.statusBarHeight;
					if (e.platform == 'android') {
						Vue.prototype.CustomBar = e.statusBarHeight + 50;
					} else {
						Vue.prototype.CustomBar = e.statusBarHeight + 45;
					};
					// #endif

					// #ifdef MP-WEIXIN
					Vue.prototype.StatusBar = e.statusBarHeight;
					let custom = wx.getMenuButtonBoundingClientRect();
					Vue.prototype.Custom = custom;
					Vue.prototype.CustomBar = custom.bottom + custom.top - e.statusBarHeight;
					// #endif		

					// #ifdef MP-ALIPAY
					Vue.prototype.StatusBar = e.statusBarHeight;
					Vue.prototype.CustomBar = e.statusBarHeight + e.titleBarHeight;
					// #endif
				}
			})

			Vue.prototype.ColorList = [{
					title: '嫣红',
					name: 'red',
					color: '#e54d42'
				},
				{
					title: '桔橙',
					name: 'orange',
					color: '#f37b1d'
				},
				{
					title: '明黄',
					name: 'yellow',
					color: '#fbbd08'
				},
				{
					title: '橄榄',
					name: 'olive',
					color: '#8dc63f'
				},
				{
					title: '森绿',
					name: 'green',
					color: '#39b54a'
				},
				{
					title: '天青',
					name: 'cyan',
					color: '#1cbbb4'
				},
				{
					title: '海蓝',
					name: 'blue',
					color: '#0081ff'
				},
				{
					title: '姹紫',
					name: 'purple',
					color: '#6739b6'
				},
				{
					title: '木槿',
					name: 'mauve',
					color: '#9c26b0'
				},
				{
					title: '桃粉',
					name: 'pink',
					color: '#e03997'
				},
				{
					title: '棕褐',
					name: 'brown',
					color: '#a5673f'
				},
				{
					title: '玄灰',
					name: 'grey',
					color: '#8799a3'
				},
				{
					title: '草灰',
					name: 'gray',
					color: '#aaaaaa'
				},
				{
					title: '墨黑',
					name: 'black',
					color: '#333333'
				},
				{
					title: '雅白',
					name: 'white',
					color: '#ffffff'
				},
			];

			uni.onTabBarMidButtonTap(function(e) {

				uni.navigateTo({
					url: '/pages/addParts/addType'
				})
			})

			// #ifdef APP-PLUS
			// 锁定竖屏  
			plus.screen.lockOrientation("portrait-primary");
			uni.request({
				url: 'https://ynf.xxhf.cn/index.php/api/common/get_app_version',
				method: 'GET',
				success: result => {
					if (result.data.code === 1) {
						plus.runtime.getProperty(plus.runtime.appid, function(inf) {
							
							let mobileType = ''
							if (plus.os.name === "Android") {
								mobileType = result.data.data.android_agent
							} else {
								mobileType = result.data.data.ios_agent
							}
							if (inf.version !== mobileType.version) {
								const dtask = plus.downloader.createDownload(mobileType.url, {force: true}, function ( d, status ) {
									if ( status === 200 ) { // 下载成功    
											var path = (d.filename);    
											plus.runtime.install(path,{},function(){
												 plus.nativeUI.closeWaiting();
												 plus.nativeUI.alert("应用资源更新完成！",function(){
														 plus.runtime.restart();
												 });
										 },function(e){
												 plus.nativeUI.closeWaiting();
												 plus.nativeUI.alert("安装wgt文件失败["+e.code+"]："+e.message);
										 });
									} else {//下载失败    
											alert( "Download failed: " + status );     
									}      
								});
								uni.showModal({  
									title:'版本更新',  
									content: mobileType.content,  
									confirmText:'下载',  
									success: (res) => {  
										if(res.confirm) {  
												dtask.start();   
												let prg = 0;  
												let showLoading = plus.nativeUI.showWaiting("正在下载");  
												dtask.addEventListener('statechanged', function(task, status) {  
														// 给下载任务设置一个监听 并根据状态  做操作  
														switch (task.state) {  
																case 1:  
																	showLoading.setTitle("正在下载");  
																	break;  
																case 3:  
																	prg = parseInt((parseFloat(task.downloadedSize) / parseFloat(task.totalSize)) * 100);  
																	showLoading.setTitle("  正在下载" + prg + "%  ");  
																	break;  
																case 4:  
																	 plus.nativeUI.closeWaiting();  
																		//下载完成  
																	break;  
														}  
												})  
										}  
									}  
								})
							}
						});
					}
				},
			})
			// #endif
		},
		onShow: function() {
			console.log('App Show')
		},
		onHide: function() {
			console.log('App Hide')
		}

	}
</script>

<style lang="scss">
	/*每个页面公共css */
	@import "colorui/main.css";
	@import "colorui/icon.css";
	@import "@/uni_modules/uview-ui/index.scss";
	@import "common/demo.scss";

	uni-modal .uni-modal__bd {
		white-space: pre-wrap;
	}
</style>
